package com.cloudspace.jindun.activity.interphone;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.ListView;

import com.android.volley.error.VolleyError;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.adapter.ChangeChannelAdapter;
import com.cloudspace.jindun.model.ChannelListResult;
import com.cloudspace.jindun.network.api.API;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.utils.ToastMaker;

import org.apache.http.HttpStatus;

/**
 * Created by zengxianhua on 17/1/25.
 */

public class ChangeChannelActivity extends CustomTitleBaseActivity{
    private ChangeChannelAdapter mChangeChannelAdapter;
    private ListView mChannelListView;
    private String mCurrentChannelId = "";

    public static int CHANNEL_CHANGE_ID = 100;

    public static final String TAG = ChangeChannelActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_changechannel, true);
        getTitleBar().setSimpleMode(getResources().getString(R.string.change_channel));

        if (getIntent() != null){
            mCurrentChannelId = getIntent().getStringExtra("curchannelid");
        }

        mChannelListView = (ListView) findViewById(R.id.channelList);
        mChangeChannelAdapter = new ChangeChannelAdapter();
        mChannelListView.setAdapter(mChangeChannelAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getChannelList();
    }

    public void getChannelList(){
        API.getInstance().getChannelApi().getChannelList(TAG, new ApiCallback<ChannelListResult>() {
            @Override
            public void handleResult(ChannelListResult result, VolleyError error) {
                if (result != null && result.status == HttpStatus.SC_OK){
                    mChangeChannelAdapter.setDatas(result.data, ChangeChannelActivity.this, mCurrentChannelId);
                }else if (error != null){
                    ToastMaker.showToastShort(error.getMessage());
                }
            }
        });
    }

    public static void showActivityForResult(Fragment context, String curChannelId){
        Intent intent = new Intent(context.getContext(), ChangeChannelActivity.class);
        intent.putExtra("curchannelid", curChannelId);
        context.startActivityForResult(intent, CHANNEL_CHANGE_ID);
    }
}
